﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Media;
using Infragistics;
using VNet.VisiSuite.Common;
using VNet.VisiSuite.Controls;
using VNet.VisiSuite.VisiBoard.Common.Helpers;
using VNet.VisiSuite.Common.Production;
using VNet.VisiSuite.VisiBoard.Models;
using VNet.VisiSuite.VisiBoard.Views;
using VNet.VisiSuite.Common.Helpers;
using VNet.VisiSuite.Service.Contact.Message.ProductionData;

namespace VNet.VisiSuite.VisiBoard.ViewModels
{
    public class DowntimeViewModel : BaseViewModel
    {
        private DowntimeModel _model;

        private List<DowntimeLineChartModel> _downtime_line_data;
        private int _maximum_line_value = 15;
        private int _line_interval;

        public DowntimeViewModel()
        {
            _model = new DowntimeModel();
            TopLeftButtonIcons = new ObservableCollection<VNetMenuItem> { new VNetMenuItem(null, "Return to previous screen", CommonConstants.VNetIcon.AppbarArrowLeft, VNetMenuItemType.WhiteBackgroundTheme, OnExit), };
            var visi_board = BusinessLogic.VisiBoard.instance();
            PageTitle = "VisiBoard | " + visi_board.AreaName + " | " + visi_board.CellName;

            CalculateLineChartData();
        }

        public string BarGraphTitle
        {
            get 
            {
                return string.Format("TOP DOWNTIME HOURS {0} - {1}", _model.ShiftStart.ToString("h:mm tt"), _model.ShiftEnd.ToString("h:mm tt"));    
            }
        }
         
        public List<DowntimeGraphDataItem> BarGraphData { get { return _model.DowntimeChartData; } }

        public Brush HeaderTextBrush
        {
            get { return Brushes.Blue; }
        }

        public Brush HeaderBrush
        {
            get { return Brushes.White; }
        }

        private void CalculateLineChartData()
        {
            var line_data = new List<DowntimeLineChartModel>();
            var start_time = _model.ShiftStart;
            var end_time = _model.ShiftEnd;

            while (start_time < end_time)
            {
                var next_time = start_time.AddHours(1);
                var duration = 0.0;
                foreach (var item in _model.DowntimeChartData)
                {
                    var e_time = item.EndTime.HasValue ? item.EndTime.Value : DateTime.Now;
                    var item_start_time = item.StartTime > start_time ? item.StartTime : start_time;

                    if (item_start_time >= next_time || (e_time <= start_time))
                        continue;
                    if (e_time > next_time)
                        duration += (next_time - item_start_time).TotalMinutes;
                    else
                        duration += (e_time - item_start_time).TotalMinutes;
                }

                line_data.Add(new DowntimeLineChartModel
                {
                    Time = start_time.ToString("h tt"),
                    Duration = duration
                });
                start_time = next_time;
            }
            LineInterval = line_data.Max(l => (int)l.Duration) / 5;
            MaximumLineValue = LineInterval*6;
            DowntimeLineData = line_data;
        }
        private double SubtractDateTime(DateTime? from, DateTime? to)
        {
            double diff = 0.0;

            if (from != null)
                diff = ((to == null ? DateTime.Now : to.Value) - from.Value).TotalHours;

            return diff;
        }



        public List<DowntimeLineChartModel> DowntimeLineData
        {
            get { return _downtime_line_data; }
            set
            {
                _downtime_line_data = value;
                OnPropertyChanged("DowntimeLineData");
            }
        }


        public DowntimeModel Model
        {
            get
            {
                return _model;
            }
            set
            {
                _model = value;
                OnPropertyChanged("Model");
            }
        }

        public int MaximumLineValue
        {
            get { return _maximum_line_value; }
            set
            {
                _maximum_line_value = value;
                OnPropertyChanged("MaximumLineValue");
            }
        }


        public int LineInterval
        {
            get { return _line_interval; }
            set
            {
                _line_interval = value;
                OnPropertyChanged("LineInterval");
            }
        }

       

        private void OnExit()
        {
            NavigationManager.NavigateTo(new ProductionView(), null);
        }

        public override void OnNavigate(object obj)
        {
        }

        public override void OnLeave()
        {
        }
    }
}
